/****************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxcustrsapi.p                                    */
/* PROCEDURE TYPE        : API export Process                               */
/* DESCRIPTION           : customer required export Process                 */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                 4-Mar-24  */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}
{us/px/pxmaint.i}
{us/px/pxphdef.i sosodxr}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical initial yes      no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram         as character                no-undo.
define variable vc_traceid            as character                no-undo.

define variable shipfrom_from         as character  no-undo.
define variable shipfrom_to           as character  no-undo.
define variable shipto_from           as character  no-undo.
define variable shipto_to             as character  no-undo.
define variable part_from             as character  no-undo.
define variable part_to               as character  no-undo.
define variable order_from            as character  no-undo.
define variable order_to              as character  no-undo.
define variable include_pastdue       as logical    no-undo.
define variable due_dt                as date    no-undo.
define variable show_reqs             as integer initial 3   no-undo.
define variable dTotalShipQty         as decimal    no-undo.
define variable net_req               as decimal    no-undo.
define variable cum_plan_qty          as decimal    no-undo.
define variable prior_net_req         as decimal    no-undo.
define variable prior_contr           as character  no-undo.
define variable prior_part            as character  no-undo.

define buffer custad for ad_mstr.
define buffer shipad for ad_mstr.
define buffer dockad for ad_mstr.

define temp-table ttSearchCondition no-undo
        field customer_from         like Debtor.DebtorCode
        field customer_to           like Debtor.DebtorCode
        field shipfrom_from         as character
        field shipfrom_to           as character
        field part_from             as character
        field part_to               as character
        field order_from            as character
        field order_to              as character
        field include_pastdue       as logical
        field due_dt                as date 
        .


 define temp-table ttcustomerrs no-undo

        field schd_site      like si_mstr.si_site
        field schd_order     like scx_ref.scx_order
        field schd_line      like scx_ref.scx_line 
        field schd_cust      like so_mstr.so_cust
        field schd_shipto    like scx_ref.scx_shipto
        field schd_dock      like sod_det.sod_dock
        field schd_part      like sod_det.sod_part
        field schd_contr_id  like sod_det.sod_contr_id
        field schd_net_req   like net_req
        field schd_date      like schd_det.schd_date
        field schd_time      like schd_det.schd_time
        field schd_reference like schd_det.schd_reference
        field schd_ord_mult  like sod_det.sod_ord_mult
        field schd_custpart  like sod_det.sod_custpart
        field schd_pkg_code  like sod_det.sod_pkg_code
        field schd_modelyr   like scx_ref.scx_modelyr
        field schd_custref   like scx_ref.scx_custref
        field schd_pcr_qty   like prior_net_req

        index idx1 schd_order schd_line schd_date schd_time.

  
   DEFINE DATASET dsSearchCondition FOR ttSearchCondition.

   hdataset = dataset dsSearchCondition:HANDLE.   
           
   DEFINE DATASET dsCustomerrs FOR ttcustomerrs .

   hresult = dataset dsCustomerrs:HANDLE.    
                
output to /apps/eai/work/cim/custrs.log.
mainloop:
repeat on error undo mainloop, leave mainloop:
  
 
   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "cm/"
       m_archpath    = m_archpath + "cm/"
       m_successpath = m_successpath + "cm/"
       m_errorpath   = m_errorpath + "cm/"
       m_workpath    = m_workpath + "cm/".


   empty temp-table ttcustomerrs.
   
   
   /* Load SEARCH conditions from JSON  */ 
   
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
                    
  if vl_RetOK then do:

    for each ttSearchCondition.
           assign shipfrom_from  = ttSearchCondition.shipfrom_from
                  shipfrom_to    = ttSearchCondition.shipfrom_to    
                  shipto_from    = ttSearchCondition.customer_from   
                  shipto_to      = ttSearchCondition.customer_to    
                  part_from      = ttSearchCondition.part_from   
                  part_to        = ttSearchCondition.part_to  
                  order_from     = ttSearchCondition.order_from  
                  order_to       = ttSearchCondition.order_to   
                  include_pastdue = ttSearchCondition.include_pastdue
                  due_dt           = ttSearchCondition.due_dt. 

     end. 

         if shipfrom_to = "" then shipfrom_to = hi_char.
         if shipto_to = "" then shipto_to = hi_char.
         if part_to = "" then part_to = hi_char.
         if order_to = "" then order_to = hi_char.
         if due_dt = ? then due_dt = today.
         
        
    for each scx_ref
        where scx_ref.scx_domain = global_domain and  scx_type       = 1
         and scx_shipfrom  >= shipfrom_from
         and scx_shipfrom  <= shipfrom_to
         and scx_shipto    >= shipto_from
         and scx_shipto    <= shipto_to
         and scx_part      >= part_from
         and scx_part      <= part_to
         and scx_order     >= order_from
         and scx_order     <= order_to
        no-lock,
     
        each sod_det where sod_det.sod_domain = global_domain
         and sod_nbr   = scx_order
         and sod_line  = scx_line
         and sod_compl_stat = ""
        no-lock,
     
       each so_mstr where so_mstr.so_domain = global_domain
         and so_nbr    = sod_nbr
         and so_cust  >= shipto_from
         and so_cust  <= shipto_to
         and so_compl_stat = ""
       no-lock,
 
   each sch_mstr
    where sch_mstr.sch_domain = global_domain and  sch_type     = 3
     and sch_nbr      = sod_nbr
     and sch_line     = sod_line
     and sch_rlse_id  = sod_curr_rlse_id[3]
 
   no-lock
         break by scx_type
         by scx_shipfrom
         by so_cust
         by scx_shipto
         by scx_order
         by sod_dock:
   
            put  scx_ref.scx_order scx_ref.scx_line scx_ref.scx_part skip .
            /* GET THE TOTAL SHIPPED FOR SCHEDULED ORDER LINES RELATED TO THIS */
   /* LINE AS DEFINED BY AN ITEM REPLACEMENT SCHEDULE.                */
   dTotalShipQty = 0.
   {us/px/pxrun.i &PROC='getCumShipQtyByOrderLineSet' &PROGRAM='sosodxr.p'
            &HANDLE=ph_sosodxr
            &PARAM="(input sod_nbr, input sod_line,
                     output dTotalShipQty)"
            &NOAPPERROR=True
            &CATCHERROR=True}
 
   if (not can-find (first schd_det
       where schd_det.schd_domain = global_domain and  schd_type      = sch_type
        and schd_nbr       = sch_nbr
        and schd_line      = sch_line
        and schd_rlse_id   = sch_rlse_id
        and schd_req_type = (if show_reqs = 3 then schd_req_type else show_reqs)))
        and dTotalShipQty < sch_pcr_qty
   then do :
 

      for first si_mstr
          where si_mstr.si_domain = global_domain and  si_site = scx_shipfrom
         no-lock:
      end. /* FOR FIRST si_mstr */
 
      for first custad
          where custad.ad_domain = global_domain and  custad.ad_addr = so_cust
         no-lock:
      end. /* FOR FIRST custad */
 
      for first shipad
          where shipad.ad_domain = global_domain and  shipad.ad_addr =
          scx_shipto
         no-lock:
      end. /* FOR FIRST shipad */
 
      for first dockad
          where dockad.ad_domain = global_domain and  dockad.ad_addr = sod_dock
         no-lock:
      end. /* FOR FIRST dockad */
      
       create ttcustomerrs.
       assign ttcustomerrs.schd_site = si_mstr.si_site
              schd_order  = scx_ref.scx_order
              schd_line   = scx_ref.scx_line 
              schd_cust   = so_mstr.so_cust
              schd_shipto = scx_ref.scx_shipto
              schd_dock   = sod_det.sod_dock
              schd_part   = sod_det.sod_part
              schd_contr_id = sod_det.sod_contr_id
              schd_pcr_qty  = sch_pcr_qty - dTotalShipQty.

              put  scx_ref.scx_order scx_ref.scx_line scx_ref.scx_part "2" skip .

     end. /* if (not can-find (first schd_det */

     for each schd_det
       where schd_det.schd_domain = global_domain and (  schd_type     =
       sch_type
        and schd_nbr      = sch_nbr
        and schd_line     = sch_line
        and schd_rlse_id  = sch_rlse_id
        and schd_req_type = (if show_reqs = 3 then schd_req_type else
                              show_reqs)
        and (
             (not include_pastdue and schd_date = due_dt)
             or
             (include_pastdue and schd_date <= due_dt)
            )
       /* CUM/DISCRETE FILTER - BEGIN */
 
        and (
             (so_cum_acct and schd_cum_qty > dTotalShipQty)
            or
             (not so_cum_acct)
            )
      ) no-lock
            break by schd_type
            by schd_nbr
            by schd_line
            by schd_rlse_id
            by schd_date
            by schd_time
            by schd_reference
            by sod_part
            by sod_contr_id:
 
                put  scx_ref.scx_order scx_ref.scx_line scx_ref.scx_part "3" skip .
      if schd_discr_qty <> 0 and
         (schd_ship_qty / schd_discr_qty * 100 <
              (if so_ship_cmplt = 0
               then
                  100
               else
                  so_ship_cmplt))
      then do:
 
         if first-of(schd_time)
         then do:
 
            for first si_mstr
                where si_mstr.si_domain = global_domain and  si_site =
                scx_shipfrom
               no-lock:
            end. /* FOR FIRST si_mstr */
 
            for first custad
                where custad.ad_domain = global_domain and  custad.ad_addr =
                so_cust
               no-lock:
            end. /* FOR FIRST custad */
 
            for first shipad
                where shipad.ad_domain = global_domain and  shipad.ad_addr =
                scx_shipto
               no-lock:
            end. /* FOR FIRST shipad */
 
            for first dockad
                where dockad.ad_domain = global_domain and  dockad.ad_addr =
                sod_dock
               no-lock:
            end. /* FOR FIRST dockad */

 
           /*
               si_site
               si_desc
               scx_order
               scx_line     
               so_cust
                custad.ad_name
               scx_shipto
               shipad.ad_name              
               sod_dock
               schd_date
               schd_time
             */
 
 
            assign
               prior_part  = ""
               prior_contr = "".
         end. /* IF FIRST-OF(schd_time) */
 
         /* CALCULATE FOR CUM/DISCRETE - BEGIN */
         /*IF THE REQUIREMENTS SELECTED ARE PLANNING REQUIREMENTS ONLY,    */
         /*USE A VARIABLE TO ACCUMULATE THE PLAN QTY INSTEAD OF USING THE  */
         /*SCHD_CUM_QTY WHICH ALSO CONTAINS CUMULATIVE SHIP QTYS.          */
         if so_cum_acct
         then do:
            if show_reqs = 1 then do:
               if first-of(schd_line) then
                  cum_plan_qty = schd_discr_qty.
               else
                  cum_plan_qty = cum_plan_qty + schd_discr_qty.
               assign
               net_req = max(min(schd_discr_qty,
                                   cum_plan_qty - dTotalShipQty),0)
               prior_net_req = max(cum_plan_qty -
                                  (dTotalShipQty + net_req),0).
            end. /*IF SHOW_REQS*/
            else do:
            assign
               net_req       = max(min(schd_discr_qty,
                                   schd_cum_qty - dTotalShipQty),0)
               prior_net_req = max(schd_cum_qty -
                                  (dTotalShipQty + net_req),0).
            end. /* ELSE DO*/
         end. /* IF SO_CUM_ACCT*/
         else
            assign
               prior_net_req = if sod_part <> prior_part
                                  or sod_contr_id <> prior_contr
                               then
                                  0
                               else
                                  prior_net_req + net_req
               prior_part    = sod_part
               prior_contr   = sod_contr_id
               net_req       = schd_discr_qty - schd_ship_qty.
         /* CALCULATE FOR CUM/DISCRETE - END */
 
         create ttcustomerrs.
         assign ttcustomerrs.schd_site = si_mstr.si_site
                ttcustomerrs.schd_order  = scx_ref.scx_order
                ttcustomerrs.schd_line   = scx_ref.scx_line 
                ttcustomerrs.schd_cust   = so_mstr.so_cust
                ttcustomerrs.schd_shipto = scx_ref.scx_shipto
                ttcustomerrs.schd_dock   = sod_det.sod_dock
                ttcustomerrs.schd_part   = sod_det.sod_part
                ttcustomerrs.schd_contr_id = sod_det.sod_contr_id
                ttcustomerrs.schd_net_req  = net_req
                ttcustomerrs.schd_date     = schd_det.schd_date
                ttcustomerrs.schd_time     = schd_det.schd_time
                ttcustomerrs.schd_reference = schd_det.schd_reference
                ttcustomerrs.schd_ord_mult  = sod_det.sod_ord_mult
                ttcustomerrs.schd_custpart = sod_det.sod_custpart
                ttcustomerrs.schd_pkg_code = sod_det.sod_pkg_code
                ttcustomerrs.schd_modelyr  = scx_ref.scx_modelyr
                ttcustomerrs.schd_custref  = scx_ref.scx_custref
                ttcustomerrs.schd_pcr_qty  = prior_net_req.
                

 
      end. /* IF SCHD_DISCR_QTY <> 0 AND .. */
 
   end. /* FOR EACH schd_det */





    end. /*  for each scx_ref */

  end.  /* if vl_RetOK then do: */ 
                    
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
      
   
   
   leave.
end. /* repeat on error undo mainloop, leave mainloop: */
